import { observable, useStrict, action } from 'mobx'
import EventEmitter from '../libs/eventEmitter'
import $ from 'jquery'

useStrict(true)

class ModalRepoFormStore {
  @observable showNameError = false
  @observable showModal = false
  @observable name = ''

  @action open() {
    this.showModal = true
  }

  @action close() {
    this.showModal = false
    this.name = ""
  }

  @action handleChange(e) {
    this.name = e.target.value
  }

  addRepo(headerStore, data) {
    headerStore.addRepo(data)
  }

  @action save(e, name, headerStore) {
    if (name.value.trim().length === 0) {
      this.showNameError = true
      return false
    } else {
      this.showNameError = false
      e.preventDefault()
      $.post('/regional_design/repos.json', { repo: {name: this.name} }, (data) => {
        this.close()
        this.addRepo(headerStore, data)
        EventEmitter.dispatch('alert', '操作成功')
      }, 'JSON')
    }
  }
}

export default new ModalRepoFormStore()
